package dao;

import db.JdbcUtil;
import javabean.Pinglun;
import javabean.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PinglunDao {

    /**
     * 添加新的评论
     * @param pinglun
     * @throws Exception
     */
    public void add(Pinglun pinglun) throws Exception {
        Connection conn = null;
        PreparedStatement ps = null;
        conn = JdbcUtil.getConnection();
        String sql = "insert into pinglun(content,name)  values (?,?) ";
        ps = conn.prepareStatement(sql);
        ps.setString(1, pinglun.getContent());
        ps.setString(2, pinglun.getName());
        ps.executeUpdate();
        JdbcUtil.free(null, ps, conn);
    }
    //查询全部用户的方法QueryAll()
    public List<Pinglun> QueryAll() throws Exception {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Pinglun> pinglunList = new ArrayList<Pinglun>();
        conn = JdbcUtil.getConnection();
        String sql = "select * from pinglun";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            int id= rs.getInt("id");
            String content= rs.getString("content");
            String name= rs.getString("name");
            Date date= rs.getDate("ctime");
            Pinglun pinglun = new Pinglun(id,content,name,date);
            pinglunList.add(pinglun);
        }
        JdbcUtil.free(rs, ps, conn);
        return pinglunList;
    }

}
